/*
	StargateWW for GarrysMod10
	Copyright (C) 2008 Prefanatic

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

credits = {}

--################# Add credits @Prefanatic
function credits.Add(ply, ammount)

	WW.Credits.Current = credits.Get(ply) -- get the current credits

	if credits.Check(ply) then -- if we can spend
		
		ply:SetNWInt("credits", WW.Credits.Current + ammount) -- set the credits
		print(ply:Nick() .. " has gained " .. ammount .. " credits.") -- log it
		
	end
	
end

--################# Get credits @Prefanatic
function credits.Get(ply)
	Wcredits = ply:GetNWInt("credits") -- grab the credits
	
	return Wcredits -- return the credits
	
end

--################# Spend credits @Prefanatic
function credits.Spend(ply, ammount)

	WW.Credits.Current = credits.Get(ply) -- get the current credits

	if credits.Check(ply) then -- if we can spend
	
		if WW.Credits.Current - ammount < 0 then return false end -- break if we will spend less than we should
	
		ply:SetNWInt("credits", WW.Credits.Current - ammount) -- set the credits
		print(ply:Nick() .. " has spent " .. ammount .. " credits.") -- log it
		return true -- return true for the check
		
	end
	
end

--################# Initialize credits @Prefanatic
function credits.Init(ply)

	print("Initializing credits for " .. ply:Nick()) -- log the initalization

	if credits.Check(ply) then -- if we can spend
	
		print("Setting default credits for " .. ply:Nick()) -- more log
		
		if WW.Credits.Spent then return false end -- if we already spent then break
		
		ply:SetNWInt("credits", WW.Credits.Default) -- set default
		
		WW.Credits.Initialized = true -- tell client that we initalized
		ply:SetNWBool("creditsinit", true)
		
	end
	
end

--################# Check if we can spend @Prefanatic
function credits.Check(ply)
	
	for k,v in pairs(WWClass) do -- grab all the classes
		if v.Team == ply:Team() and v.Leader then -- if we are a leader
			print("Found " .. ply:Nick() .. ", he is leader.")
			
			return true -- return true
			
		end
		
	end
	
end